package com.utaedang.jesseclub.db;

import com.utaedang.jesseclub.utils.GlobalContext;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	private static final String TAG = "DBAdapter";

	public DBAdapter(Context ctx){
		this.context = ctx;
		DBHelper = new DatabaseHelper(this.context);
		try {
			DBHelper.createDatabase();
			DBHelper.openDataBase();
		} catch (IOException e) {
			Log.d("DBAdapter", e.toString());
		} catch (SQLException e) {
			Log.d("DBAdapter", e.toString());
		}
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		private SQLiteDatabase sqlite;
		private final Context crContext;
		
		DatabaseHelper(Context context){
			super(context, GlobalContext.DB_NAME, null, GlobalContext.DB_VERSION);
			this.crContext = context;
		}
		
		public void createDatabase() throws IOException {
			if (!checkDatabase()){
				this.getReadableDatabase();
				try {
					copyDatabase();
				} catch (IOException e) {
					throw new Error("Error copying database");
				}
			}
		}
		
		public boolean checkDatabase() {
			SQLiteDatabase checkDB = null;
			try{
				String myPath = GlobalContext.DB_PATH + GlobalContext.DB_NAME;
				checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
	   
			} catch (SQLiteException e) {
				Log.d("checkDatabase", e.toString());
			}
	 
			if(checkDB != null){
				checkDB.close();
			}
			return checkDB != null ? true : false;
		}
		
		private void copyDatabase() throws IOException {
		    InputStream myInput = crContext.getAssets().open(GlobalContext.DB_NAME);
		 
		    String outFileName = GlobalContext.DB_PATH + GlobalContext.DB_NAME;
		    OutputStream myOutput = new FileOutputStream(outFileName);
		 
		    byte[] buffer = new byte[1024];
		    int length;
		    while ((length = myInput.read(buffer))>0){
		    	myOutput.write(buffer, 0, length);
		    }
		    myOutput.flush();
		    myOutput.close();
		    myInput.close();
		}
		
		public void openDataBase() throws SQLException{
			String myPath = GlobalContext.DB_PATH + GlobalContext.DB_NAME;
			sqlite = SQLiteDatabase.openDatabase(myPath, null,  SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		}
		
		@Override
		public synchronized void close() {
		       if(sqlite != null)
		        sqlite.close();
		       super.close();
		}
	 
		@Override
		public void onCreate(SQLiteDatabase db) {
			//기본 TABLE 생성
			/*
			db.execSQL("Create TABLE " + TABLE_NAME + "(" + 
					COLUMN1 + " TEXT PRIMARY KEY," + 
					COLUMN2 + " TEXT," + 
					COLUMN3 + " TEXT);");
					*/
		}
		 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
			        + newVersion + ", which will destroy all old data");          
			        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);    
			onCreate(db);
		}  
	}

	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		DBHelper.close();
	}
}
